/**
 * 状态字	含义
 * 0xZZ	模式	蜂鸣器	报警器
 * 0x80	自由	关闭	关闭
 * 0x88	自由	关闭	启动
 * 0x90	自由	启动	关闭
 * 0x98	自由	启动	启动
 * 0x40	训练	关闭	关闭
 * 0x48	训练	关闭	启动
 * 0x50	训练	启动	关闭
 * 0x58	训练	启动	启动
 * 0x20	考核	关闭	关闭
 * 0x28	考核	关闭	启动
 * 0x30	考核	启动	关闭
 * 0x38	考核	启动	启动
 * 0x07	保持系统当前状态
 */

package com.oschina;

import org.omg.PortableInterceptor.DISCARDING;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Protocol {

    public static final byte START = (byte)0x55;
    public static final byte END = (byte)0xFF;
    public static final byte CONNECTED = (byte)0x01;
    public static final byte BREAKED = (byte)0x00;

    //DIRECTIVE
    public static final byte D_SETSTATUS_OR_CONFIRM = (byte)0xAA;
    public static final byte D_START_CHECK = (byte)0xB0;
    public static final byte D_END_CHECK = (byte)0xB1;
    public static final byte D_BEEP = (byte)0xCC;
    public static final byte D_FLASH = (byte)0xDD;

    //STATUS
    public static final byte S_01 =(byte)0x80;//自由	关闭	关闭
    public static final byte S_02 =(byte)0x88;//自由	关闭	启动
    public static final byte S_03 =(byte)0x90;//自由	启动	关闭
    public static final byte S_04 =(byte)0x98;//自由	启动	启动
    public static final byte S_05 =(byte)0x40;//训练	关闭	关闭
    public static final byte S_06 =(byte)0x48;//训练	关闭	启动
    public static final byte S_07 =(byte)0x50;//训练	启动	关闭
    public static final byte S_08 =(byte)0x58;//训练	启动	启动
    public static final byte S_09 =(byte)0x20;//考核	关闭	关闭
    public static final byte S_10 =(byte)0x28;//考核	关闭	启动
    public static final byte S_11 =(byte)0x30;//考核	启动	关闭
    public static final byte S_12 =(byte)0x38;//考核	启动	启动
    public static final byte S_13 =(byte)0x07;//保持系统当前状态

    public static final byte[] DIRECTIVE_LIST = {S_01,S_02,S_03,S_04,S_05,S_06,S_07,S_08,S_09,S_10,S_11,S_12,S_13
    ,D_START_CHECK,D_END_CHECK,D_BEEP};

    /**
     * 获取指令
     *
     * @param x 状态字
     * @return 指令数组
     */
    public static byte[] getSendProtocol(byte x) {
        byte[] message = new byte[6];
        message[0] = START;
        message[1] = (byte)0x00;
        message[2] = (byte)0x00;
        message[3] = x;
        message[4] = (byte)0x07;
        message[5] = END;
        return message;
    }

    /**
     * 获取指令
     *
     * @param x 0xXX设备地址
     * @param y 0xYY设备地址
     * @return 指令
     */
    public static byte[] getSendProtocol(byte x, byte y) {
        byte[] message = new byte[6];
        message[0] = START;
        message[1] = x;
        message[2] = y;
        message[3] = (byte)0xAA;
        message[4] = 0x07;
        message[5] = END;
        return message;
    }

    /**
     * 解析消息
     *
     * @param msg 解析消息
     */
    public static void resoveMessage(Object[] msg) {
        System.out.println("message:  " + msg[4]);
        switch ((byte)msg[4]) {
            case (byte)0xAA:
                System.out.println("系统初始化完毕：" + msg[4]);
                break;
            case (byte)0xB5:
                System.out.println("确认信号：" + msg[4]);
                break;
            case (byte)0xBA:
                System.out.println("取消信号：" + msg[4]);
                break;
            case CONNECTED:
                System.out.println(msg[1] + "," + msg[2] + "号设备当前状态为：已连接");
                break;
            case BREAKED:
                System.out.println(msg[1] + "," + msg[2] + "号设备当前状态为：断开");
                break;
        }
    }

}